<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container style="">
        <avue-crud
          ref="crud"
          :data="tableData"
          :permission="permissionList"
          :table-loading="tableLoading"
          :option="tableOption"
          @on-load=""
          @search-change="searchChange"
          @refresh-change="refreshChange"
          @row-update="handleUpdate"
          @row-save="handleSave"
          @row-del="handleDel"
          @sortable-change="sortableChange">
<!--
          <template slot="menuLeft">
            <el-button  type="primary"  v-if=" lineInfo.lineStatus=='未生产'" @click="startProduction">开始生产<i class="el-icon-video-play el-icon&#45;&#45;right"></i> </el-button>
            <el-button  type="primary"  v-if=" lineInfo.lineStatus=='生产中'" @click="pauseProduction">暂停<i class="el-icon-video-pause el-icon&#45;&#45;right"></i> </el-button>
            <el-button  type="primary"  v-if=" lineInfo.lineStatus=='暂停中'" @click="continueProduction">继续生产<i class="el-icon-video-play el-icon&#45;&#45;right"></i> </el-button>
          </template>
-->
          <template slot-scope="{row,index}" slot="menu">
            <el-button type="text"  v-if="row.moStatus !=='生产中'" icon="el-icon-video-play" size="small" @click="startProduction(row,index)">开始生产</el-button>
            <el-button type="text"  v-if="row.moStatus ==='生产中'" icon="el-icon-switch-button" size="small" @click="endProduction(row,index)">完工</el-button>

            <el-button type="text"  icon="el-icon-document" size="small" @click="workOrderReportDetails(row,index)">报工详情</el-button>
            <el-button type="text"  icon="el-icon-document" size="small" @click="zzpReportDetails(row,index)">在制品报废明细</el-button>
            <el-button type="text"  icon="el-icon-document" size="small" @click="wlReportDetails(row,index)">物料报废明细</el-button>
            <el-button type="text"  icon="el-icon-document" size="small" @click="reworkReportDetails(row,index)">返工原因明细</el-button>
          </template>
          <!--产线自定义搜索-->
          <template slot-scope="scope" slot="lineIdSearch">
            <div class="block">
              <el-select  v-model="lineId" filterable placeholder="请选择产线">
                <el-option
                  @click.native="chooseLine(item)"
                  v-for="item in lineList"
                  :label="item.realityLineName"
                  :value="item.id"
                  :key="item.id">
                </el-option>
              </el-select>
            </div>
          </template>
        </avue-crud>
      <!--报工详情目录弹窗页面-->
      <el-dialog title=""
                 :visible.sync="reportDialogVisible"
                 width="50%"
                 lock-scroll
                 v-if="reportDialogVisible"
                 :fullscreen="reportDialogFull">
        <el-backtop target=".el-card__body .el-dialog__wrapper .el-dialog" :right="15" :bottom="20" :visibility-height="1" />
        <el-backtop :bottom="60"></el-backtop>
        <template slot="title">
          <div class="avue-crud__dialog__header">
            <span class="el-dialog__title">
                <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                {{title}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>工单：{{docNo}}——品号：{{itemCode}}
            </span>
          </div>
        </template>
        <!--报工详情页面-->
        <workOrderReportDetailsPage v-if="flg==='gd'" ref="reportRef"  :docNo="docNo"/>
        <!--在制品详情页面-->
        <zzpReportDetailsPage v-if="flg==='zzp'" ref="zzpReportRef" :docNo="docNo"/>
        <!--物料报废详情页面-->
        <wlReportDetailsPage v-if="flg==='wl'" ref="wlReportRef" :docNo="docNo"/>
        <!--返工原因详情页面-->
        <reworkReportDetailsPage v-if="flg==='rework'" ref="reworkReportRef" :docNo="docNo"/>

      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import {addObj, putObj, delObj,
         getWorkOrderListByLineId,//获取产线下工单
         workOrderSort,//工单排序
} from '@/api/workorder/workordercps'
import {startProduct,endProduct} from "@/api/workorder/workorderreport";//开始生产
import { getLineList} from '@/api/pfoc/realityproductlinemain' //获取产线集合
import { remote} from '@/api/admin/dict' //字典接口
import { sortableOption } from '@/const/crud/workorder/workordersort' //行拖拽排序option
import { mapGetters } from 'vuex'
import workOrderReportDetailsPage from "@/views/workorder/workorderreport/index.vue";
import zzpReportDetailsPage from "@/views/workorder/workinprogressscrapdetails/index.vue";
import wlReportDetailsPage from "@/views/workorder/workordermaterialscrapdetails/index.vue";
import reworkReportDetailsPage from "@/views/workorder/reworkdetails/index.vue";


export default {
  name: 'workordercps',
  components: {
    workOrderReportDetailsPage,
    zzpReportDetailsPage,
    wlReportDetailsPage,
    reworkReportDetailsPage
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      tableLoading: false,
      tableOption: sortableOption,
      lineList:[],//产线集合
      lineId:null,//产线ID
      lineInfo:{},//产线数据对象
      workshopSectionList:[],//工段集合
      workOrderProductionList:[],//产线生产信息
      reportDialogVisible:false,//报工详情页面开关
      reportDialogFull:true,//报工详情页面弹窗缩放开关（默认放大）
      title:null,
      docNo:null,//工单号
      itemCode:null,//品号
      flg:null,

    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.workorder_workordercps_add, false),
        delBtn: this.vaildData(this.permissions.workorder_workordercps_del, false),
        editBtn: this.vaildData(this.permissions.workorder_workordercps_edit, false)
      }
    }
  },
  created() {
      //获取产线集合
      this.getLineListData();

  },
  methods: {
    // 删除
    handleDel: function (row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function (row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function (row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      // this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      // this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/workorder/workordercps/export', this.searchForm, 'workordercps.xlsx')
    },

    //获取产线集合
    getLineListData() {
      getLineList().then(response => {
        this.lineList = response.data.data;
      })
    },
    //获取工段
    getWorkshopSectionListData() {
      remote("workshop_section").then(response => {
        this.workshopSectionList = response.data.data;
      })
    },
    //产线选择
    chooseLine(row) {
      this.lineId = row.id;//产线ID
      this.lineInfo = row;//当前选择的产线对象
      this.getList();//获取当前产线下的未完工的排序工单
    },

    // 工单排序列表查询
    getList() {
      if (this.validatenull(this.lineId)) {
        this.$message.warning("请选择产线。")
        return;
      }
      this.tableLoading = true
      //根据产线ID查询产线下工单
      getWorkOrderListByLineId(this.lineId, null).then(response => {
        this.tableData = response.data.data
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      });
    },
    //拖拽排序
    sortableChange(oldindex, newindex, row, newList, oldList) {
      //循环数据列表为排序字段赋值
      newList.forEach((item, index) => {
        item.sortOrder = index;
        newList[index] = item;
      });
      var data = {
        workorderList: newList,
      }
      //提交
      workOrderSort(data).then(response => {
        this.tableData = newList;//更新产线下工单数据
        this.$message.success("排序成功")
      }).catch(() => {
        this.$message.error("排序失败")
      });

    },
    //开始生产
    startProduction(row) {
      if (!this.validatenull(this.tableData)) {
        //改变工单状态为生产中
        startProduct(this.lineId,row.docNo,"生产中").then(response => {
          //更新表数据
          this.getList();
        })
      } else {
        this.$message.warning("当前产线下暂无工单任务.")
      }
    },
    //完工
    endProduction(row) {
      //完工
      endProduct(this.lineId, row.docNo,"已完工").then(response => {
        //更新表数据以及工段信息
        this.getList();
      })
    },

    //报工详情
    workOrderReportDetails(row) {
      this.reportDialogVisible = true;
      this.docNo = row.docNo;
      this.itemCode = row.itemCode;
      this.title = "工单报工详情";
      this.flg = "gd";
      this.$nextTick(() => {
        //调用报工详情查询方法
        this.$refs.reportRef.refreshChange();
      })
    },
    //在制品报工详情
    zzpReportDetails(row,index){
      this.reportDialogVisible = true;
      this.docNo = row.docNo;
      this.itemCode = row.itemCode;
      this.title = "在制品不良明细";
      this.flg = "zzp";
      this.$nextTick(() => {
        //调用报工详情查询方法
        this.$refs.zzpReportRef.refreshChange();
      })
    },
    //物料报工详情
    wlReportDetails(row,index){
      this.reportDialogVisible = true;
      this.docNo = row.docNo;
      this.itemCode = row.itemCode;
      this.title = "物料不良明细";
      this.flg = "wl";
      this.$nextTick(() => {
        //调用报工详情查询方法
        this.$refs.wlReportRef.refreshChange();
      })
    },
    //返工详情
    reworkReportDetails(row,index){
      this.reportDialogVisible = true;
      this.docNo = row.docNo;
      this.itemCode = row.itemCode;
      this.title = "返工原因明细";
      this.flg = "rework";
      this.$nextTick(() => {
        //调用报工详情查询方法
        this.$refs.reworkReportRef.refreshChange();
      })
    },

  }
}
</script>
